package com.qdu.dao;

import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Random;

/**
 *
 * @author Lenovo
 */
public class AddManagerDao {

    private static int SUCCESS = 1;
    private static int ERROR = 2;
    private static int SAME = 3;

    public int addManager(String admin_name, String admin_password) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Random random = new Random();
        int r = random.nextInt(100);

        try {
            con = DatabaseUtil.getConnection();

            // 查看是否重名
            ps = con.prepareStatement("select * from systemadministrator where admin_name=?");
            ps.setString(1, admin_name);
            rs = ps.executeQuery();
            con.commit();

            if (rs.next()) {
                return this.SAME;
            } else {
                ps = con.prepareStatement("insert into systemadministrator(admin_name,admin_password,admin_addtime,admin_id) values(?,?,?,?)");
                ps.setString(1, admin_name);
                ps.setString(2, admin_password);
                ps.setString(3, new java.util.Date().toLocaleString());
                ps.setInt(4, r);

                int i = ps.executeUpdate();
                if (i == 1) {
                    return this.SUCCESS;
                } else {
                    return this.ERROR;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
    }

    public int delUser(int id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("delete  commonmember   where  member_id=?");
            ps.setInt(1, id);
            ps.executeUpdate();

            return this.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
    }

}
